package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;
import zozopo.flow.operations.RecordlistTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.PurchaselistOS;
import zozopo.warehouse.operations.PurchaselistTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class CancelPurchaselistAction extends ActionSupport implements Action,SessionAware{
	//传入参数
	private int id;
	
	//传出参数
	private String operationResult;
	private PurchaselistTB purchaselist;
	private List<PurchaseTB> list;
	
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		///权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
		
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
		PurchaselistOS purchaselistOS = new PurchaselistOS(conn);
		PurchaseOS purchaseOS = new PurchaseOS(conn);
		
		
		
		
		purchaselist = purchaselistOS.getObjById(id);
		list = purchaseOS.getListByPurchaselistId(id);
		
		
		
		
		Flow flow = new Flow(conn);
		//flow.getFlowValueMapByKind(kind)
		RecordlistTB recordlist = flow.getRecordlist(FlowCode.WAREHOUSE_PURCHASELIST, id);
		if(recordlist.getNextvalue_recordlist() != 1){  //最初状态 nextvalue = 1 改变后非1 , 不可取消
			operationResult = "进仓单流程状态已经改变,无法取消.如需取消需要把流程取消或把出仓流程回到最初已打出仓单状态!";
			
			databaseManager.releaseConnection(conn);
			return SUCCESS;
		}//*/
		

		
		if(!purchaselist.getUseremail_purchaselist().equals(operator.getEmail_operator())){  //如果不是操作人, 不可取消
			
			databaseManager.releaseConnection(conn);
			return NONE;
		}
		
		
		int sumNum = purchaseOS.sumNumberByPurchaselistId(id);
		int sumReserve = purchaseOS.sumReserveByPurchaselistId(id);
		if(sumNum != sumReserve){ //如果库存不等于进货，表示些进仓单属性已经改变将无法取消 , 不可取消
			operationResult = "库存不等于进货，表示些进仓单属性已经改变将无法取消";
			
		}else{
			try {
				boolean sqlError = false;
				
				conn.setAutoCommit(false);//事务开始
				List<PurchaseTB> purchaseList = purchaseOS.getListByPurchaselistId(id);  //得到些进仓单所以进仓数据
				
				for(PurchaseTB purchase:purchaseList){ //取消进仓数据
					if(!purchaseOS.cancle(purchase.getId_purchase())){
						sqlError = true;
					}
					if(!purchaseOS.updateGoodsAttribute(purchase.getGoodsid_purchase(),purchase.getUseremail_purchase())){
						sqlError = true;
					}
				}
				if(!purchaselistOS.cancle(id)){
					sqlError = true;
				}

				if(sqlError){
					conn.rollback();//事务回滚
				}else{
					conn.commit();//提交事务
					operationResult = "单号" + id + "已经被取消！";
				}
				
				
				
				purchaselist = purchaselistOS.getObjById(id);
				list = purchaseOS.getListByPurchaselistId(id);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				try {
					conn.rollback();//事务回滚
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}finally{
				try {
					conn.setAutoCommit(true);//事务结束
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				
				databaseManager.releaseConnection(conn);
			}
		}

		return SUCCESS;
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getOperationResult() {
		return operationResult;
	}
	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}
	public PurchaselistTB getPurchaselist() {
		return purchaselist;
	}
	public void setPurchaselist(PurchaselistTB purchaselist) {
		this.purchaselist = purchaselist;
	}
	public List<PurchaseTB> getList() {
		return list;
	}
	public void setList(List<PurchaseTB> list) {
		this.list = list;
	}

}
